package cmsapi

import (
	// "net/http"
	"encoding/json"
	"strconv"
	// "time"
	// "github.com/dgrijalva/jwt-go"
	"github.com/labstack/echo"
	"gopkg.in/mgo.v2/bson"
	// "github.com/jinzhu/gorm"
	
	"oxy_server/module/log"
	"oxy_server/model"
	"oxy_server/until"
)

type (
	samplepackage struct {
		ID bson.ObjectId `json:"_id" bson:"_id"`
		Gender 						string `json:"gender" bson:"gender"`
		OrderColor 					string `json:"ordercolor" bson:"ordercolor"`
		Material				 	string `json:"material" bson:"material"`
		MaterialDiagram				string `json:"materialdiagram" bson:"materialdiagram"`
		Craft 						string `json:"craft" bson:"craft"`
		WashingMethod 				string `json:"washingmethod" bson:"washingmethod"`
		Cost		 				string `json:"cost" bson:"cost"`
		ReferenceBrand 				string `json:"referencebrand" bson:"referencebrand"`
		MostImpressiveSellPoint		string `json:"mostimpressivesellpoint" bson:"mostimpressivesellpoint"`
		Note					  	string `json:"note" bson:"note"`
	}
	insertsamplepackage struct {
		Gender 						string `json:"gender" bson:"gender"`
		OrderColor 					string `json:"ordercolor" bson:"ordercolor"`
		Material				 	string `json:"material" bson:"material"`
		MaterialDiagram				string `json:"materialdiagram" bson:"materialdiagram"`
		Craft 						string `json:"craft" bson:"craft"`
		WashingMethod 				string `json:"washingmethod" bson:"washingmethod"`
		Cost		 				string `json:"cost" bson:"cost"`
		ReferenceBrand 				string `json:"referencebrand" bson:"referencebrand"`
		MostImpressiveSellPoint		string `json:"mostimpressivesellpoint" bson:"mostimpressivesellpoint"`
		Note					  	string `json:"note" bson:"note"`
	}
)

func GetSamplePackageListHandler(c echo.Context) error {
	currentpage,err := strconv.Atoi(c.QueryParam("currentpage"))
	perpage,err := strconv.Atoi(c.QueryParam("perpage"))
	jsonquery := c.QueryParam("like")
	if err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	model.DB().Dbname="OxyCms"
	model.DB().Document="sample_package"
	M, err := until.JsonToBson(jsonquery)
	if err != nil {
		log.Error("json-> bson 失败",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	query,count,err:=model.DB().Query(M, currentpage, perpage)
	if err != nil {
		log.Error("查询出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	ps:=[]samplepackage{}
	query.All(&ps)
	
	str, err := json.Marshal(ps)
	if err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	c.JSON(200, map[string]interface{}{
		"msg":   "success",
		"data":string(str),
		"count":count,
	})

	return nil
}

func AddSamplePackageHandler(c echo.Context) error {
	s:= &insertsamplepackage{}
	if err := c.Bind(s); err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}
	model.DB().Dbname="OxyCms"
	model.DB().Document="sample_package"
	err:=model.DB().Insert(s)
	if err != nil {
		log.Error("插入出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg":   "success",
	})

	return nil	
}
func UpdateSamplePackageHandler(c echo.Context) error {
	id := c.QueryParam("id") 
	log.Info("UpdateSamplePackageHandler",id)
	
	s:= &insertsamplepackage{}
	if err := c.Bind(s); err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}
	model.DB().Dbname="OxyCms"
	model.DB().Document="sample_package"
	err:= model.DB().Update(bson.M{"_id":bson.ObjectIdHex(id)} , s)
	if err != nil{
		log.Error("更新出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg":  "success",
	})

	return nil
}
func DelSamplePackageHandler(c echo.Context) error {
	id := c.QueryParam("id")
	log.Info("DelSamplePackageHandler",id)
	model.DB().Dbname="OxyCms"
	model.DB().Document="sample_package"
	err:=model.DB().Remove(bson.M{"_id":bson.ObjectIdHex(id)})
	if err != nil{
		log.Error("删除出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":  "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg":  "success",
	})

	return nil
}